var MJModel           = require('./MJModel');
var MJProto           = require('./MJProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
		tickLabel: cc.Label,
		dongNode: cc.Node,
		xiNode: cc.Node,
		nanNode: cc.Node,
		beiNode: cc.Node,
		dirsNode: cc.Node,
		countLabel: cc.Label,
	},

	onLoad: function () {
		this.tick = 0;
		this.isOn = false;
		for (let item of [this.dongNode, this.xiNode, this.nanNode, this.beiNode]) {
			item.active = false;
		}
		this.tickLabel.node.active = false;
		this.node.active = false; 

		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
	},

	gameInit: function () {
		if (MJModel.getGameStatus() == MJProto.gameStatus.NONE && MJModel.getCurBureau() == 0) {
			this.node.active = false;
		}
		else {
			this.node.active = true;
		}
		this.setRotation();
		this.setDir();
		this.setTick();
		this.countLabel.string = '剩'+MJModel.getRestCardsCount()+'张';
	},

	setDir: function () {
		let nodes = [this.dongNode, this.nanNode, this.xiNode, this.beiNode];
		for (let item of nodes) {
			item.active = false;
		}
		let curChairID = MJModel.getCurChairID();
		if (curChairID != null) {
			if (MJModel.getChairCount() != 2) {
				nodes[curChairID].active = true;
			}
			else {
				if (curChairID == 0) {
					nodes[curChairID].active = true;
				}
				else {
					nodes[2].active = true;
				}
			}
		}
	},

	setRotation: function () {
		let myChairID = MJModel.getMyChairID();
		if (myChairID == 0) {
			this.dirsNode.rotation = 0;
		}
		else if (myChairID == 1) {
			if (MJModel.getChairCount() != 2) {
				this.dirsNode.rotation = 90;
			}
			else {
				this.dirsNode.rotation = 180;
			}
		}
		else if (myChairID == 2) {
			this.dirsNode.rotation = 180;
		}
		else if (myChairID == 3) {
			this.dirsNode.rotation = 270;
		}
	},

	setTick: function (tick) {
		this.tick = MJModel.getTick();
		this.isOn = true;
		this.tickLabel.node.active = true;
	},

	messageCallbackHandler: function(router, msg) {
		if(!MJModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === MJProto.GAME_TURN_PUSH) {
				this.setTick();
				this.setDir();
			}
			else if (msg.type == MJProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus == MJProto.gameStatus.NONE) {
					let nodes = [this.dongNode, this.nanNode, this.xiNode, this.beiNode];
					for (let item of nodes) {
						item.active = false;
					}
					this.countLabel.string = '剩余牌数:'+MJModel.getRestCardsCount();
				}
				else {
					this.node.active = true;
				}
			}
			else if (msg.type == MJProto.GAME_REST_CARDS_COUNT_PUSH) {
				this.countLabel.string = '剩余牌数:'+MJModel.getRestCardsCount();
			}
		} 
	},

	update: function(dt) {
		if (!this.isOn || MJModel.isDismissing()) {
			return;
		}
		this.tick -= dt;
		let tick = Math.floor(this.tick);
		tick = (tick < 0)? 0:tick;
		tick = (tick < 10)? '0'+tick:tick;
		this.tickLabel.string = tick;
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
	},
});
